package gui;

import java.io.File;
import javax.swing.filechooser.*;

public class SongFilter extends FileFilter {

    //Accept all directories and all mp3s
    public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }

        //get the extension of the file
        String extension = getExtension(f);

        if (extension.equalsIgnoreCase("mp3"))
        {
            return true;
        }
        //default -- fall through. False is return on all
        //occasions except:
        //a) the file is a directory
        //b) the file's extension is what we are looking for.
        return false;

    }

    //The description of this filter
    public String getDescription() {
        return "MP3 files only";
    }

    /**
    Method to get the extension of the file, in lowercase
     */
    private String getExtension(File f)
    {
        String s = f.getName();
        int i = s.lastIndexOf('.');
        if (i > 0 && i < s.length() - 1)
        {
            return s.substring(i + 1).toLowerCase();
        }
        return "";
    }

}
